<?php
class pCategoryAction extends pBaseAction {

    public function index(){
        $dao=new CategoryModel();
        $this->assign('allCategorys',$dao->getTree());
        $this->display();
    }

    public function edit(){
        $catid=$this->getSid('catid');
        if(false!==$catid){
            $dao=new CategoryModel();
            $rs=$dao->where("id={$catid}")->find();
            if(false!==$rs){
                $this->assign('rs',$rs);
                $this->assign('allCategorys',$dao->getTree());
                return $this->display();
            }
        }
        return $this->fail(L('edit_fail'),L('find_fail'));
    }

    public function update(){
        $dao=new CategoryModel();
        $t=array_key_exists('t',$_POST) ? $_POST['t'] :'1';
        if('2'===$t){
            //move category
        }else{
            $result=$dao->updateCategory();
            if(false!==$result){
                $this->ajaxReturn('success');
            }else{
                $this->ajaxReturn('failure',$dao->getError());
            }
        }
    }

    public function add(){
        $dao=new CategoryModel();
        $this->assign('allCategorys',$dao->getTree());
        $this->display();
    }

    public function save(){
        $dao=new CategoryModel();
        $result=$dao->addCategory();
        if(false!==$result){
            $this->ajaxReturn('success');
        }else{
            $this->ajaxReturn('failure',$dao->getError());
        }
    }

    /**
     * 栏目授权
     */
    public function allocate(){/*{{{*/
        $prefix=C('db_prefix');
        if($this->isGet()){     //是GET请求，则说明是要选择用户--查找拥有pContent相关权限的用户
            $dao=new Model();
            $users=$dao->field('distinct u.id,u.account,u.realname')
                ->table("{$prefix}user u,{$prefix}node n, {$prefix}access a")
                ->where("a.parentNodeId=n.id and u.role=a.role and n.name='pContent'")
                ->findAll();
            $this->assign('users', $users);
            $this->display('allocate_chooseuser');
        }else if($this->isPost()){
            $step=$_POST['step'];
            if('chooseuser'==$step){    //选择用户
                $uarr=explode(',', $this->getFirstSid());   //TODO:没有去验证用户是否有权限,也没有去验证传入参数是否正确
                $dao=new Model();
                //查询用户的授权情况
                $allocate=$dao->field('category,allocate')->table($prefix.'category_user')->where("userid='{$uarr[0]}'")->findAll();
                $this->assign('allocate', $allocate);
                //列出可用栏目---不列出跳转栏目
                $cates=$dao->field('name,id,level')
                    ->where("jump='' and id>1")
                    ->table($prefix.'category')
                    ->order('lft')
                    ->findAll();
                $this->assign('cates', $cates);
                $this->assign('uarr',$uarr);
                $this->display('allocate_choosecategory');
            }else if('choosecategory'==$step){
                $sid=$this->getSid();
                if($sid){
                    $add=array();
                    $uid=$_POST['uid'];
                    $range=array();
                    if(array_key_exists('range', $_POST)){
                        $range=$_POST['range'];
                    }
                    foreach($sid as $id){   //遍历选中的栏目
                        $add[]=array(
                            'category'=>$id,
                            'userid'=>$uid,
                            'allocate'=>(in_array($id,$range))?'y':'n'
                        );
                    }
                    $dao=new CategoryUserModel();
                    $dao->startTrans();
                    //先删除旧有的授权数据，再添加新的数据
                    if($dao->deleteAll("userid='{$uid}'")!==false && $dao->addAll($add)!==false ){
                        $dao->commit();
                        $this->ok('栏目授权成功','成功设定了用户<b>'.$uid.'</b>的授权栏目',array(
                            array('继续栏目授权','allocate')
                        ));
                    }else{
                        $dao->rollback();
                        $this->fail('栏目授权失败', '对用户<b>'.$uid.'</b>的栏目授权失败');
                    }
                }
            }
        }
    }/*}}}*/

}
